package com.vm.db;

import com.vm.model.Setting;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;

public class SettingDao {
	private static final String TAG = "SettingDao";

	public SettingDao(Context context) {
		if (DBOpenHelper.db == null) {
			new DBOpenHelper(context);
		}
	}

	public synchronized void update1(Integer nopic) {
		Log.d(TAG, "update1");
		try {
			DBOpenHelper.db.execSQL("UPDATE setting set nopic=?",
					new Integer[] { nopic });
		} catch (SQLException e) {
			Log.d(TAG, "update1---" + e.getMessage());
		}
	}

	public synchronized void update2(Integer onlywifi) {
		Log.d(TAG, "update2");
		try {
			DBOpenHelper.db.execSQL("UPDATE setting set onlywifi=?",
					new Integer[] { onlywifi });
		} catch (SQLException e) {
			Log.d(TAG, "update2---" + e.getMessage());
		}
	}

	public synchronized void update3(Integer zeroflow) {
		Log.d(TAG, "update3");
		try {
			DBOpenHelper.db.execSQL("UPDATE setting set zeroflow=?",
					new Integer[] { zeroflow });
		} catch (SQLException e) {
			Log.d(TAG, "update3---" + e.getMessage());
		}
	}

	public synchronized void update4(Integer autoinstall) {
		Log.d(TAG, "update4");
		try {
			DBOpenHelper.db.execSQL("UPDATE setting set autoinstall=?",
					new Integer[] { autoinstall });
		} catch (SQLException e) {
			Log.d(TAG, "update4---" + e.getMessage());
		}
	}

	public synchronized void update5(Integer autodelete) {
		Log.d(TAG, "update5");
		try {
			DBOpenHelper.db.execSQL("UPDATE setting set autodelete=?",
					new Integer[] { autodelete });
		} catch (SQLException e) {
			Log.d(TAG, "update5---" + e.getMessage());
		}
	}

	public synchronized void update6(Integer quickinstall) {
		Log.d(TAG, "update6");
		try {
			DBOpenHelper.db.execSQL("UPDATE setting set quickinstall=?",
					new Integer[] { quickinstall });
		} catch (SQLException e) {
			Log.d(TAG, "update6---" + e.getMessage());
		}
	}

	public synchronized void update7(Integer updatenotification) {
		Log.d(TAG, "update7");
		try {
			DBOpenHelper.db.execSQL("UPDATE setting set updatenotification=?",
					new Integer[] { updatenotification });
		} catch (SQLException e) {
			Log.d(TAG, "update7---" + e.getMessage());
		}
	}

	public synchronized void update8(Integer autorotation) {
		Log.d(TAG, "update8");
		try {
			DBOpenHelper.db.execSQL("UPDATE setting set autorotation=?",
					new Integer[] { autorotation });
		} catch (SQLException e) {
			Log.d(TAG, "update8---" + e.getMessage());
		}
	}

	public synchronized Setting query() {
		Log.d(TAG, "query");
		Cursor c = null;
		Setting setting = null;
		try {
			c = DBOpenHelper.db
					.rawQuery(
							"SELECT nopic,onlywifi,zeroflow,autoinstall,autodelete,quickinstall,updatenotification,autorotation FROM setting",
							new String[] {});
			setting = null;
			if (c.moveToNext()) {
				setting = new Setting();
				setting.setNopic(c.getInt(0));
				setting.setOnlywifi(c.getInt(1));
				setting.setZeroflow(c.getInt(2));
				setting.setAutoinstall(c.getInt(3));
				setting.setAutodelete(c.getInt(4));
				setting.setQuickinstall(c.getInt(5));
				setting.setUpdatenotification(c.getInt(6));
				setting.setAutorotation(c.getInt(7));
			}
		} catch (Exception e) {
			Log.d(TAG, "query---" + e.getMessage());
		} finally {
			if (c != null) {
				c.close();
			}
		}
		return setting;
	}
}